package cc.md888.app.application.user;

import cc.md888.app.application.activatecode.ActivateCodeService;
import cc.md888.app.domain.entity.ActivateCode;
import cc.md888.app.domain.entity.User;
import cc.md888.app.enums.UserStatusEnum;
import cc.md888.app.exception.BizRuntimeException;
import cc.md888.app.infr.dao.UserDao;
import cc.md888.app.model.user.UserLoginCmd;
import cc.md888.app.model.user.EmailRegistrationCmd;
import cc.md888.app.model.user.LoginResultVO;
import cc.md888.app.model.user.RegistrationResultVO;
import cc.md888.result.ResultCode;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@Component
public class UserService {

    @Autowired
    private ActivateCodeService activateCodeService;

    @Autowired
    private UserDao userDao;


    /**
     * 使用邮箱注册账号
     * @param cmd
     * @return
     */
    public RegistrationResultVO registerByEmail(EmailRegistrationCmd cmd){
        User user = userDao.getByEmail(cmd.getEmail());
        if(user != null){
            throw BizRuntimeException.from(ResultCode.ERROR, "邮箱已存在");
        }

        ActivateCode activateCode = activateCodeService.getByToken(cmd.getToken());
        if(activateCode==null){
            throw BizRuntimeException.from(ResultCode.ERROR, "没有找到验证码");
        }

        if(!Objects.equals(cmd.getCode(), activateCode.getCode())){
            throw BizRuntimeException.from(ResultCode.ERROR, "验证码不正确");
        }

        LocalDateTime now = LocalDateTime.now();
        if (now.isAfter(activateCode.getExpireAt())) {
            throw BizRuntimeException.from(ResultCode.ERROR, "验证码已过期");
        }


        if (!Objects.equals(cmd.getPassword(), cmd.getConfirmPassword())) {
            throw BizRuntimeException.from(ResultCode.ERROR, "两次输入的密码不一致");
        }

        User newUser = new User();
        newUser.setEmail(cmd.getEmail());
        newUser.setId(IdWorker.getId());
        newUser.setAccessToken(UUID.randomUUID().toString().replace("-",""));
        newUser.setPassword(DigestUtils.md5Hex(cmd.getPassword()));
        newUser.setStatus(UserStatusEnum.ACTIVE.name());
        userDao.save(newUser);


        RegistrationResultVO registrationResultVO = new RegistrationResultVO();
        registrationResultVO.setEmail(newUser.getEmail());
        registrationResultVO.setAccessToken(newUser.getAccessToken());
        registrationResultVO.setId(newUser.getId());
        return registrationResultVO;
    }


    /**
     * 用户登录
     */
    public LoginResultVO login(UserLoginCmd cmd){
        User user = userDao.getByEmail(cmd.getEmail());
        if(user == null){
            throw BizRuntimeException.from(ResultCode.ERROR, "用户不存在");
        }

        String encryptedPassword = DigestUtils.md5Hex(cmd.getPassword());
        if(!Objects.equals(cmd.getPassword(), encryptedPassword)){
            throw BizRuntimeException.from(ResultCode.ERROR, "密码不正常，请重新输入");
        }

        if(Objects.equals(user.getStatus(), UserStatusEnum.BAN.name())){
            throw BizRuntimeException.from(ResultCode.ERROR, "该用户已被封禁");
        }

        LoginResultVO loginResultVO = new LoginResultVO();
        loginResultVO.setEmail(user.getEmail());
        loginResultVO.setAccessToken(UUID.randomUUID().toString().replace("-",""));
        loginResultVO.setId(user.getId());
        return loginResultVO;
    }

}
